"""
@Project ：Python_Learning 
@File    ：20.有效的括号.py
@IDE     ：PyCharm 
@Author  ：zhupp
@Date    ：2024/10/15 16:22 
"""

class Solution:
    def isValid(self, s: str) -> bool:
        stack = []
        valid = {
            '(': ')',
            '[': ']',
            '{': '}',
            ')': '(',
            ']': '[',
            '}': '{',
        }
        if len(s) < 2:
            return False
        for char in s:
            if char == '(' or char == '[' or char == '{':
                stack.append(char)
            if len(stack) != 0 and (char == '}' or char == ']' or char == ')') and stack[-1] == valid[char]:
                stack.pop()
            elif (char == '}' or char == ']' or char == ')') and len(stack) == 0:
                return False
            elif (char == '}' or char == ']' or char == ')') and stack[-1] != valid[char]:
                return False
        if len(stack) == 0:
            return True
        else:
            return False


solu = Solution()
print(solu.isValid("){}{}{"))



